#!/bin/bash
#
# Copyright 2013 Joyent Inc., All rights reserved.
#

set -e

ROOT=$(cd "$(dirname "$0")/.." && pwd)

SVCCFG="${ROOT}/projects/illumos/usr/src/cmd/svc/svccfg/svccfg-native"

rdmnt=$1
if [ -z "${rdmnt}" ] || [ ! -d "${rdmnt}" ]; then
  echo "FATAL: Invalid ramdisk root '${rdmnt}'"
  echo "Usage: $0 <rdmnt>"
  exit 1
fi

if [[ ! -x ${SVCCFG} ]]; then
	echo "FATAL: Missing svccfg-native binary from illumos build looked in '${SVCCFG}'"
	exit 1
fi

# TODO check that we're root

REPO_DB=${rdmnt}/etc/svc/repository.db
export SVCCFG_CHECKHASH=1
export PKG_INSTALL_ROOT=${rdmnt}
export SVCCFG_DTD=${rdmnt}/usr/share/lib/xml/dtd/service_bundle.dtd.1
export SVCCFG_REPOSITORY=${REPO_DB}
export SVCCFG_CONFIGD_PATH=/lib/svc/bin/svc.configd

${SVCCFG} -v repository ${REPO_DB}
${SVCCFG} -v add smf/manifest
${SVCCFG} -v import ${rdmnt}/lib/svc/manifest
${SVCCFG} -s "system/boot-config:default" setprop config/fastreboot_default=false
${SVCCFG} -s "system/auditd:default" setprop general/enabled=true
${SVCCFG} -s "system/coreadm:default" setprop config_params/global_enabled=true
${SVCCFG} -s "system/coreadm:default" setprop config_params/global_setid_enabled=true
${SVCCFG} -s "system/coreadm:default" setprop config_params/global_pattern='/%Z/cores/core.%f.%p'

# These two are only for use in zones.
${SVCCFG} -s "smartdc/mdata:fetch" setprop general/enabled=false
${SVCCFG} -s "smartdc/mdata:execute" setprop general/enabled=false

# ${SVCCFG} -s "system/manifest-import" setprop start/exec=:true
${SVCCFG} -v apply ${rdmnt}/etc/svc/profile/generic.xml
${SVCCFG} -v apply ${rdmnt}/etc/svc/profile/platform.xml

# rcap imports with enabled=false, but applying generic.xml enables it.
${SVCCFG} -s "rcap:default" setprop general/enabled=false
# make sure mail svcs enabled after we've applied generic.xml
${SVCCFG} -s "network/smtp:sendmail" setprop general/enabled=true
${SVCCFG} -s "network/sendmail-client:default" setprop general/enabled=true

exit 0
